<?php

use Izqut\Membership\Models\Membership;

if (!function_exists('membership')) {
    /**
     * Get the current user's active membership
     *
     * @return \Izqut\Membership\Models\UserMembership|null
     */
    function membership()
    {
        if (!auth()->check()) {
            return null;
        }

        return auth()->user()->membership()->first();
    }
}

if (!function_exists('is_vip')) {
    /**
     * Check if the current user is VIP
     *
     * @return bool
     */
    function is_vip(): bool
    {
        if (!auth()->check()) {
            return false;
        }

        return auth()->user()->isVip();
    }
}

if (!function_exists('has_membership')) {
    /**
     * Check if the current user has an active membership
     *
     * @param string|null $slug Optional membership slug to check
     * @return bool
     */
    function has_membership(?string $slug = null): bool
    {
        if (!auth()->check()) {
            return false;
        }

        if ($slug) {
            return auth()->user()->hasMembershipPlan($slug);
        }

        return auth()->user()->hasMembership();
    }
}

if (!function_exists('membership_plans')) {
    /**
     * Get all active membership plans
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    function membership_plans()
    {
        return Membership::active()->ordered()->get();
    }
}
